package com.pb.wkflow.api.service.impl;

import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.pb.infra.oth.entity.common.Result;
import com.pb.infra.oth.service.BaseService;
import com.pb.infra.oth.util.UserInfo;
import com.pb.wkflow.api.controller.assembler.ClassifyAssembler;
import com.pb.wkflow.api.controller.request.ClassifyQueryRequest;
import com.pb.wkflow.api.entity.WfSysClassifyEntity;
import com.pb.wkflow.api.exception.ApprovalException;
import com.pb.wkflow.api.mapper.WfClassifyMapper;
import com.pb.wkflow.api.service.WfClassifyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 流程分类服务
 * @author 苗益辉
 * @date 2022/9/8 11:11
 */
@Slf4j
@Service
public class WfClassifyServiceImpl extends BaseService implements WfClassifyService {

    @Autowired
    WfClassifyMapper wfClassifyMapper;

    /**
     * 查询
     * @param request request
     * @param pageSize 页数
     * @param pageIndex 页码
     * @return Result
     */
    @Override
    public Result list(ClassifyQueryRequest request, Integer pageIndex, Integer pageSize) {
        UserInfo userInfo = this.getCurrentUserInfo();
        if (pageIndex != null && pageSize != null) {
            PageMethod.startPage(pageIndex, pageSize);
        }
        WfSysClassifyEntity entity = ClassifyAssembler.assemblerToQuery(request,userInfo);
        List<WfSysClassifyEntity> list = wfClassifyMapper.selectList(entity);
        return Result.ok(new PageInfo<WfSysClassifyEntity>(list).getTotal(), list);
    }

    /**
     * 保存
     * @param request request
     * @return Result
     */
    @Override
    public Result save(ClassifyQueryRequest request) {
        UserInfo userInfo = this.getCurrentUserInfo();
        WfSysClassifyEntity wfSysClassifyEntity = wfClassifyMapper.selectOne(
                request.getClassifyCode(), userInfo.getCorpId());
        if (wfSysClassifyEntity != null) {
            throw new ApprovalException("该流程分类编码已存在");
        }
        WfSysClassifyEntity entity = ClassifyAssembler.assemblerToSave(request, userInfo);
        wfClassifyMapper.insert(entity);
        return Result.ok("保存成功");
    }

    /**
     * 更新
     * @param request request
     * @return Result
     */
    @Override
    public Result update(ClassifyQueryRequest request) {
        String cropId = this.getCurrentUserInfo().getCorpId();
        WfSysClassifyEntity wfSysClassifyEntity = wfClassifyMapper.selectOne(request.getClassifyCode(), cropId);
        if (wfSysClassifyEntity != null && !request.getId().equals(wfSysClassifyEntity.getId())) {
            throw new ApprovalException("该流程分类编码已存在");
        }
        if (wfSysClassifyEntity == null || !request.getId().equals(wfSysClassifyEntity.getId())) {
            wfSysClassifyEntity = wfClassifyMapper.selectById(request.getId());
        }
        WfSysClassifyEntity entity = ClassifyAssembler.assemblerToUpdate(request, wfSysClassifyEntity);
        wfClassifyMapper.update(entity);
        return Result.ok("修改成功");
    }

    @Override
    public Result getAbleList(Integer isAble) {
        String cropId = this.getCurrentUserInfo().getCorpId();
        return Result.ok( wfClassifyMapper.getAbleList(cropId, isAble));
    }
}
